using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;

public class AssetsDepHandler : XMLHandler
{
    Dictionary<string, string[]> AssetDepList;
    public AssetsDepHandler(Dictionary<string, string[]> list)
    {
        AssetDepList = list;
    }

    string currentName;
    List<string> DepList = new List<string>();
    public override void elementStart(string element, XMLAttributes attributes)
    {
        if (element == "Asset")
        {
            currentName = attributes.getValue("name");
        }
        else if (element == "Deps")
        {
            DepList.Add(attributes.getValue("name"));
        }
    }

    public override void elementEnd(string element)
    {
        if (element == "Asset")
        {
            AssetDepList[currentName] = DepList.ToArray();
            currentName = null;
            DepList.Clear();
        }
        else if (element == "Deps")
        {

        }
    }

    static string GetDepth(int depth)
    {
        string s = "";
        while ((depth--) > 0)
        {
            s += "  ";
        }

        return s;
    }

    public static void SaveToFile(Dictionary<string, string[]> DepList, string filename)
    {
        StreamWriter Write = new StreamWriter(filename, false, System.Text.Encoding.UTF8);
        Write.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
        Write.WriteLine("<AssetRoot>");

        foreach (KeyValuePair<string, string[]> itor in DepList)
        {
            if (itor.Value.Length == 0)
            {
                Write.WriteLine("{0}<Asset name = \"{1}\" />", GetDepth(1), itor.Key);
            }
            else
            {
                Write.WriteLine("{0}<Asset name = \"{1}\">", GetDepth(1), itor.Key);
                foreach (string dep in itor.Value)
                {
                    Write.WriteLine("{0}<Deps name = \"{1}\" />", GetDepth(2), dep);
                }

                Write.WriteLine("{0}</Asset>", GetDepth(1));
            }
        }

        Write.WriteLine("</AssetRoot>");
        Write.Close();
    }

    public static void UpdateToFile(string name, string[] deps, string filename)
    {
        Dictionary<string, string[]> DepList = new Dictionary<string, string[]>();
        DepList[name] = deps;
        UpdateToFile(DepList, filename);
    }

    public static void UpdateToFile(Dictionary<string, string[]> DepList, string filename)
    {
        Dictionary<string, string[]> AssetDepList;
        if (File.Exists(filename))
        {
            FileStream file = null;
            try
            {
                file = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
                AssetDepList = new Dictionary<string, string[]>();
                int size = (int)file.Length;
                byte[] buffer = new byte[size];
                file.Read(buffer, 0, size);
                XMLParser.parseXMLFile(new AssetsDepHandler(AssetDepList), new MemoryStream(buffer));

                foreach (KeyValuePair<string, string[]> itor in DepList)
                {
                    AssetDepList[itor.Key] = itor.Value;
                    Logger.LogDebug("Key:{0} Value:{1}", itor.Key, itor.Value);
                }
            }
            catch(System.Exception e)
            {
                AssetDepList = DepList;
                Logger.LogError("UpdateToFile error!:{0}", e.Message);
            }

            if (file != null)
                file.Close();
        }
        else
        {
            AssetDepList = DepList;
        }

        try
        {
            SaveToFile(AssetDepList, filename);
        }
        catch(System.Exception e)
        {
            Logger.LogError("SaveToFile error!:{0}", e.Message);
        }
    }
}